<?php

namespace app\admin\controller;

use app\admin\model\LinkModel;
use app\admin\model\RoomModel;
use app\admin\model\SchoolModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;
use think\Request;

class RoomController extends AdminBaseController
{

    public function index()
    {
        $name = $this->request->param('name');
        $school_id=$this->request->param('school_id');
       $school=Db::name('school')->select();
        $room = Db::name('room')
            ->join('cmf_school', 'cmf_room.sid=cmf_school.id', 'LEFT')
            ->join('cmf_student','cmf_student.cid=cmf_room.id')
            ->join('cmf_record','cmf_record.stu_id=cmf_student.id')
            ->join('cmf_score','cmf_score.id=cmf_record.score_id')
            ->field('cmf_school.name as school_name')
            ->field('cmf_room.*')
            ->field('sum(cmf_score.name) as zongfen')
            ->group('cmf_room.id')
            ->where(function (Query $query) use ($name,$school_id) {
                if ($name) {
                    $query->where('cmf_room.name', 'like', "%$name%");
                }
                if ($school_id){
                    $query->where('sid',$school_id);
                }
            })
            ->order('id DESC')
            ->paginate(3, false, ['query' => $this->request->param()]);
        $page = $room->render();
        return view('index', ['room' => $room, 'page' => $page,'school'=>$school,'school_id'=>$school_id]);


    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            //halt($data);
            if (empty($data['name'])) {
                $this->error('班级不能为空，请输入班级');
            }
            $add = RoomModel::create($data);
            if ($add) {
                $this->success('添加成功', 'room/index');
            } else {
                $this->error('添加失败', 'room/index');
            }
        }
        $school = SchoolModel::all();
        return view('add', ['school' => $school]);
    }


    public function edit($id)
    {
        $school = Db ::name('school')->select();
        $room = Db ::name('room')->get($id);
        if(request()->isPost()){
            //halt(input());
            if(empty(input('name'))){
                $this->error('班级不能为空');
            }
            $edit = Db :: name('room')->update(input());
            if($edit){
                $this->success('修改成功','room/index');
            }$this->error('修改失败');
        }
        return $this->fetch('',['school'=>$school,'room'=>$room]);
    }



    public function delete($id)
    {
        //
        RoomModel::destroy($id);
    }
    public function delAll()
    {
        $ids = input('ids/a');
        $del = RoomModel::destroy($ids);
        if ($del){
            $this->success('失败成功','room/index');
            }$this->error('删除失败');


    }
    public function mingxi($id)
    {
        $mingxi = Db::name('record')
            ->join('cmf_student','cmf_student.id=cmf_record.stu_id','LEFT')
            ->join('cmf_score','cmf_score.id=cmf_record.score_id','LEFT')
            ->join('cmf_reason','cmf_reason.id=cmf_record.reason_id','LEFT')
            ->join('cmf_room','cmf_student.cid=cmf_room.id')
            ->field('cmf_record.*,cmf_student.name as student_name,cmf_score.name as score_name,cmf_reason.name as reason_name')
            ->field('cmf_room.name as room_name')
            ->where('cmf_room.id',$id)
            ->select();
        //halt($mingxi);
        return $this->fetch('',['mingxi'=>$mingxi]
        );
    }
}
